<?php

namespace app\controllers;

use app\models\WeUser;
use DOMDocument;
use DOMXPath;
use Yii;
use app\models\Price;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * PriceController implements the CRUD actions for Price model.
 */
class PriceController extends Controller
{
    public $layout = 'foot';

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    private $we_user = null;
    private $we_app = null;

    public function beforeAction($action)
    {
        if(Yii::$app->wechat->isWechat) {
            if( !Yii::$app->wechat->isAuthorized()) {
                return Yii::$app->wechat->authorizeRequired()->send();
            }
            $this->we_user = Yii::$app->wechat->user;
            $this->we_app =  Yii::$app->wechat->app;

            $user = new WeUser();//微信登录自动保存用户信息
            $user->attributes = $this->we_user->original;
            $user->save();
        }
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    /**
     * Lists all Price models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Price::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Price model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Price model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
//    public function actionCreate()
//    {
//        $model = new Price();
//
//        if ($model->load(Yii::$app->request->post()) && $model->save()) {
//            return $this->redirect(['view', 'id' => $model->id]);
//        } else {
//            return $this->render('create', [
//                'model' => $model,
//            ]);
//        }
//    }

    /**
     * Updates an existing Price model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
//    public function actionUpdate($id)
//    {
//        $model = $this->findModel($id);
//
//        if ($model->load(Yii::$app->request->post()) && $model->save()) {
//            return $this->redirect(['view', 'id' => $model->id]);
//        } else {
//            return $this->render('update', [
//                'model' => $model,
//            ]);
//        }
//    }

    /**
     * Deletes an existing Price model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
//    public function actionDelete($id)
//    {
//        $this->findModel($id)->delete();
//
//        return $this->redirect(['index']);
//    }

    /**
     * Finds the Price model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Price the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Price::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /*
     * 单品柱状图
     */
    public function actionOneDayBar($name = '白条鸡')
    {
        $date = date('Y-n-j',time()-86400);
        $danpin = Price::find()->where("name = '{$name}' and date = '{$date}'")->orderBy('price_jin desc')->all();
        $names = Price::find()->select('name')->where("date = '{$date}'")->groupBy('name' )->asArray()->all();
        //var_dump($names);die;
        return $this->render('one', [
            'date' => $date,
            'names' => $names,
            'danpin' => $danpin,
        ]);
    }

    /*
     * 品类市场价格一览
     */
    public function actionOneDayTable($name = '白条鸡', $date = null )
    {
        if($date == null) {
            $date = date('Y-m-d',time()-86400);
        }
        $danpin = Price::find()->where("name = '{$name}' and date = '{$date}'")->orderBy('price_jin')->all();
        $names = Price::find()->select('name')->where("date = '{$date}'")->groupBy('name' )->asArray()->all();
        return $this->render('one-day-table', [
            'date' => $date,
            'names' => $names,
            'name' => $name,
            'danpin' => $danpin,
        ]);
    }

    /*
     * 单品一次性链接
     */
    public function actionOneDayTableOnce($name = '白条鸡', $date = null ,$time = '')
    {
        if($time !== date('md',time())){
            return '<html>
                    <head>
                    <script type="text/javascript">
                    function display_alert()
                      {
                      alert("该链接已失效，请加客服微信( xxmygzh001 )获取最新链接!")
                      }
                      display_alert();
                    </script>
                    </head>
                    <body>
                    </body>
                    </html>';
        }
        if($date == null) {
            $date = date('Y-m-d',time()-86400);
        }
        $danpin = Price::find()->where("name = '{$name}' and date = '{$date}'")->orderBy('price_jin')->all();
        $names = Price::find()->select('name')->where("date = '{$date}'")->groupBy('name' )->asArray()->all();
        return $this->render('one-day-table', [
            'date' => $date,
            'names' => $names,
            'name' => $name,
            'danpin' => $danpin,
        ]);
    }

    /*
     * 市场品类价格一览
     */
    public function actionMarketDayTable($market = '河南万邦国际农产品物流股份有限公司', $date = null )
    {
        if($date == null) {
            $date = date('Y-m-d',time()-86400);
        }
        $danpin = Price::find()->where("market = '{$market}' and date = '{$date}'")->orderBy('price_jin')->all();
        $markets = Price::find()->select('market')->where("date = '{$date}'")->groupBy('market' )->asArray()->all();
        return $this->render('market-day-table', [
            'date' => $date,
            'markets' => $markets,
            'market' => $market,
            'danpin' => $danpin,
        ]);
    }
}
